using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Robot.Business;
using Robot.Data;
using Robot.Entities;
using DevExpress.Web.ASPxGridView;
using Scb.Framework;
using System.IO;
using WebRobot.App_code;
using WebRobot.DataSet;

namespace WebRobot.Pages
{
  public partial class ReportPage : BasePage
  {

    #region Event

    protected void Page_Load(object sender, EventArgs e)
    {

      if (dateFrom.Value == null)
      {
        dateFrom.Value = DateTime.Today;
      }
      if (dateTo.Value == null)
      {
        dateTo.Value = DateTime.Now;
      }


    }

    protected void btnShowReport_Click(object sender, EventArgs e)
    {
      FillDataToReport();
    }

    protected void rptImportTran_Load(object sender, EventArgs e)
    {
      FillDataToReport();
    }

    #endregion

    #region Helper

    private void FillDataToReport()
    {
      DateTime from = DateTime.Parse(dateFrom.Value.ToString());
      DateTime to = DateTime.Parse(dateTo.Value.ToString());
      to.AddHours((Double)DateTime.Now.Hour);
      to.AddMinutes((Double)DateTime.Now.Minute);

      int tranApprovedNo = 0;
      int tranRunNo = 0;
      importTransaction dtImport = new importTransaction();


      tblOpicsTransactionCollection listOpicsTran = tblOpicsTransactionService.GettblOpicsTransactionListByStatus(2);

      myReport rpt = new myReport();
      tblImportTransactionCollection listTran = tblImportTransactionService.GetImportTransactionByCreatedDate(from, to, CurrentUser.Peoplewise);
      string dateFormat = "MM/dd/yyyy";

      foreach (tblImportTransaction tran in listTran)
      {
        DataRow row = dtImport.Tables["dtTransaction"].NewRow();

        if (tran.Status.Value == 3)
        {
          tranApprovedNo++;
          row["IsAppr"] = "Y";
        }
        else
        {
          row["IsAppr"] = "N";
        }
        if (listOpicsTran.Count == 0)
        {
          row["Run"] = "N";
        }
        else
        {
          foreach (tblOpicsTransaction opics in listOpicsTran)
          {
            if (tran.ID.CompareTo(opics.ImportTransactionID) == 0)
            {
              tranRunNo++;
              row["Run"] = "Y";
              row["DealNo"] = opics.DealNo;
              break;
            }
            else
            {
              row["Run"] = "N";
            }
          }
        }
        row["CreatedDate"] = tran.CreatedDate.Value.ToString(dateFormat);
        row["ValueDate"] = tran.ValueDate.Value.ToString(dateFormat);
        row["BuyCCY"] = tran.BuyCurrency;
        row["BuyAmount"] = tran.BuyAmount.Value.ToString("N2");
        row["SellCCY"] = tran.SellCurrency;
        row["SellAmount"] = tran.SellAmount.Value.ToString("N2");
        row["CusRate"] = tran.CusRate.Value.ToString("N2");
        row["Branch"] = tran.Branch;

        dtImport.Tables["dtTransaction"].Rows.Add(row);

      }

      rpt.Parameters["parFromDate"].Value = from.ToString(dateFormat);
      rpt.Parameters["parToDate"].Value = to.ToString(dateFormat);
      rpt.Parameters["parCountTran"].Value = listTran.Count.ToString();

      rpt.Parameters["parApproved"].Value = tranApprovedNo.ToString();
      rpt.Parameters["parRun"].Value = tranRunNo.ToString();

      rpt.DataSource = dtImport;

      rptImportTran.Report = rpt;
      rptImportTran.DataBind();
    }

    #endregion

  }
}
